var KOC = require("koc.common.utils");
var ExcelExport = require('excel-export');
var StateData = require("../statedata");
var DALFactory = require("../../lib/dal/dal-factory");
var CommonUtils = require("../../lib/common-utils");
var _UserData;

module.exports = {
    //财务管理
    Main: function (req, res, callback) {
        res.render("finance-manage/main", callback);
    },
    //艺人结算
    Anchor_Manage: {
        /////////////////////////////
        //提现列表
        ////////////////////////////
        Cash: function (req, res, callback) {
            res.render("finance-manage/anchor-manage/cash", callback);
        },
        /////////////////////////////
        //提现导出
        ////////////////////////////
        Cash_ExportExcel: function (req, res) {
            req.params.iDisplayLength = 100000;//默认十万条
            var reqModel = CommonUtils.Common.AjaxPage.Request(req);
            var _UserData = StateData.Value(req, res);
            //是否登录
            if (!_UserData.IsLogin) {
                res.jsonp(CommonUtils.Common.AjaxPage.ErrorCode("请登录后使用此功能", reqModel.Echo));
                return;
            }
            //判断权限
            if (!_UserData.IsAdmin) {
                res.jsonp(CommonUtils.Common.AjaxPage.ErrorCode("没有权限", reqModel.Echo));
                return;
            }
            //查询参数
            var reqParm = new DALFactory.User.Page.OrderWithdraw_Parm();
            if (reqModel.Filter_Value.length > 0) {
                switch (reqModel.Filter_Type) {
                    case "Order_No":
                        //单据号
                        reqParm.Order_No = reqModel.Filter_Value;
                        break;
                    case "User_ID":
                        //单据号
                        reqParm.User_ID = reqModel.Filter_Value;
                        break;
                    case "NickName":
                        //单据号
                        reqParm.NickName = reqModel.Filter_Value;
                        break;
                    default:
                        reqParm.FuzzyQuery = reqModel.Filter_Value;
                        break;
                }
            }
            reqParm.User_ID = req.params.F_User_ID ? KOC.StringUtils.ToInt(req.params.F_User_ID) : (reqParm.User_ID ? reqParm.User_ID : null);
            reqParm.Order_No = req.params.F_Order_No ? req.params.F_Order_No : null;
            reqParm.Account_BankName = req.params.F_Account_BankName ? req.params.F_Account_BankName : null;
            reqParm.Account_BankFullName = req.params.F_Account_BankFullName ? req.params.F_Account_BankFullName : null;
            reqParm.Account_RealName = req.params.F_Account_RealName ? req.params.F_Account_RealName : null;
            reqParm.Account_CardNo = req.params.F_Account_CardNo ? req.params.F_Account_CardNo : null;
            reqParm.IDCard = req.params.F_IDCard ? req.params.F_IDCard : null;
            reqParm.Update_Date_Between = KOC.StringUtils.ToInt(req.params.F_Update_Date_Between);
            reqParm.Update_Date_And = KOC.StringUtils.ToInt(req.params.F_Update_Date_And);
            reqParm.Create_Date_Between = req.params.F_StartDate ? req.params.F_StartDate : null;
            reqParm.Create_Date_And = req.params.F_EndDate ? req.params.F_EndDate : null;
            reqParm.Status = req.params.F_Status == '0' || req.params.F_Status == '1' ? KOC.StringUtils.ToInt(req.params.F_Status) : null;
            //按月份的上中下旬查询，传入2016-06
            if (req.params.F_Create_Date && req.params.F_Create_Date != "" && req.params.F_Type) {
                switch (req.params.F_Type) {
                    case "上旬":
                        reqParm.Create_Date_Between = req.params.F_Create_Date ? KOC.Moment(req.params.F_Create_Date)._d : null;
                        reqParm.Create_Date_And = req.params.F_Create_Date ? KOC.Moment(req.params.F_Create_Date).add(10, 'd')._d : null;
                        break;
                    case "中旬":
                        reqParm.Create_Date_Between = req.params.F_Create_Date ? KOC.Moment(req.params.F_Create_Date).add(10, 'd')._d : null;
                        reqParm.Create_Date_And = req.params.F_Create_Date ? KOC.Moment(req.params.F_Create_Date).add(20, 'd')._d : null;
                        break;
                    case "下旬":
                        reqParm.Create_Date_Between = req.params.CreF_Create_Dateate_Date ? KOC.Moment(req.params.F_Create_Date).add(20, 'd')._d : null;
                        reqParm.Create_Date_And = req.params.F_Create_Date ? KOC.Moment(req.params.F_Create_Date).add(1, 'M')._d : null;
                        break;
                    default:
                        res.jsonp(CommonUtils.Common.AjaxPage.ErrorCode("传入类型错误：Type" + req.params.F_Type));
                        return;
                        break;
                }
            }
            //排序
            switch (reqModel.Sort_Col) {
                case "User_ID":
                case "Order_No":
                case "Update_Date":
                case "Create_Date":
                    break;
            }
            DALFactory.User.Page.OrderWithdraw(reqParm, reqModel, function (retValue) {
                if (retValue.hasError) {
                    res.jsonp(CommonUtils.Common.AjaxPage.ErrorCode(retValue.message + "(" + retValue.errorCode + ")", reqModel.Echo));
                    return;
                }
                var conf = {};
                conf.cols = [
                    {caption: '申请提现时间', type: 'string'},
                    {caption: '用户ID', type: 'number'},
                    {caption: '公会ID', type: 'number'},
                    {caption: '昵称', type: 'string'},
                    {caption: '兑换收入', type: 'number'},
                    {caption: '扣税金额', type: 'number'},
                    {caption: '实发金额', type: 'number'},
                    {caption: '开户名称', type: 'string'},
                    {caption: '开户行账号', type: 'string'},
                    {caption: '开户银行', type: 'string'},
                    {caption: '开户地（省）', type: 'string'},
                    {caption: '开户地（市）', type: 'string'},
                    {caption: '身份号', type: 'string'},
                    {caption: '开户地分行地址', type: 'string'},
                    {caption: '状态', type: 'string'}
                ];
                conf.rows = [];
                KOC.Lodash.forEach(retValue.returnObject, function (ThisValue) {
                    var month = KOC.Moment(ThisValue.Create_Date).format('YYYY-MM');
                    if (parseInt(KOC.Moment(ThisValue.Create_Date).format('DD')) >= 1 && parseInt(KOC.Moment(ThisValue.Create_Date).format('DD')) <= 10) {
                        month += "-上旬";
                    } else if (parseInt(KOC.Moment(ThisValue.Create_Date).format('DD')) >= 11 && parseInt(KOC.Moment(ThisValue.Create_Date).format('DD')) <= 20) {
                        month += "-中旬";
                    } else {
                        month += "-下旬";
                    }
                    var Status = "";
                    switch (ThisValue.Status) {
                        case 0:
                            Status = "已结算";
                            break
                        case 1:
                            Status = "结算中";
                            break;
                        default:
                            Status = "-";
                            break;
                    }
                    conf.rows.push([
                        month,
                        ThisValue.User_ID,
                        ThisValue.Family_ID,
                        ThisValue.NickName,
                        ThisValue.Money,
                        ThisValue.Revenue_Money,
                        ThisValue.Real_Money,
                        ThisValue.Account_RealName,
                        ThisValue.Account_CardNo,
                        ThisValue.Account_BankName,
                        ThisValue.Account_Province,
                        ThisValue.Account_City,
                        ThisValue.IDCard,
                        ThisValue.Account_BankFullName,
                        Status
                    ]);
                });
                var result = ExcelExport.execute(conf);
                res.setHeader('Content-Type', 'application/vnd.openxmlformats');
                res.setHeader("Content-Disposition", "attachment; filename=" + "Anchor_Base.xlsx");
                res.end(result, 'binary');
            });
        },
        /////////////////////////////
        //提现结算
        ////////////////////////////
        Cash_Edit: function (req, res, callback) {
            var _PageData = {
                flag: 0,
                Order_No: KOC.StringUtils.ToInt(req.params.id, -1)
            };
            if (_PageData.Order_No <= 0) {
                CommonUtils.Common.PageScript_PageData(res, _PageData);
                res.render("finance-manage/anchor-manage/cash-edit", callback);
                return;
            }

            KOC.Async.series([
                //艺人结算列表
                function (callback) {
                    DALFactory.User.Order_Withdraw.Info(_PageData.Order_No, function (retValue) {
                        if (retValue.hasError) {
                            callback(retValue);
                            return;
                        }
                        _PageData.Order_No = retValue.returnObject.Order_No;
                        _PageData.User_ID = retValue.returnObject.User_ID;
                        _PageData.Money = retValue.returnObject.Money;
                        _PageData.Revenue_Money = retValue.returnObject.Revenue_Money;
                        _PageData.Real_Money = retValue.returnObject.Real_Money;
                        _PageData.Update_Date = retValue.returnObject.Update_Date;
                        _PageData.Create_Date = retValue.returnObject.Create_Date;
                        _PageData.Status = retValue.returnObject.Status;
                        callback(null);
                    });
                },

                //用户信息列表
                function (callback) {
                    DALFactory.User.User_Info.Info(_PageData.User_ID, function (retValue) {
                        if (retValue.hasError) {
                            callback(retValue);
                            return;
                        }
                        _PageData.NickName = retValue.returnObject.NickName;
                        _PageData.Sex = retValue.returnObject.Sex;
                        _PageData.RealName = retValue.returnObject.RealName;
                        _PageData.Email = retValue.returnObject.Email;
                        _PageData.Phone = retValue.returnObject.Phone;
                        _PageData.VIP = retValue.returnObject.VIP && (!retValue.returnObject.VIP_Expire || KOC.Moment().isBefore(retValue.returnObject.VIP_Expire));
                        _PageData.EXP = retValue.returnObject.EXP;
                        _PageData.Level = CommonUtils.User.Level.fnLevel(retValue.returnObject.EXP);
                        _PageData.Coin = retValue.returnObject.Coin;
                        _PageData.Coin_Today = retValue.returnObject.Coin_Today;
                        _PageData.Coin_Week = retValue.returnObject.Coin_Week;
                        _PageData.Coin_Month = retValue.returnObject.Coin_Month;
                        _PageData.Coin_Year = retValue.returnObject.Coin_Year;
                        _PageData.Coin_Total = retValue.returnObject.Coin_Total;
                        _PageData.Beans = retValue.returnObject.Beans;
                        _PageData.Beans_Today = retValue.returnObject.Beans_Today;
                        _PageData.Beans_Week = retValue.returnObject.Beans_Week;
                        _PageData.Beans_Month = retValue.returnObject.Beans_Month;
                        _PageData.Beans_Year = retValue.returnObject.Beans_Year;
                        _PageData.Beans_Total = retValue.returnObject.Beans_Total;
                        _PageData.Fans = retValue.returnObject.Fans;
                        _PageData.Attention = retValue.returnObject.Attention;
                        _PageData.IsAnchor = retValue.returnObject.IsAnchor;
                        _PageData.IsFamily = retValue.returnObject.IsFamily;
                        _PageData.Login_Count = retValue.returnObject.Login_Count;
                        _PageData.Login_This_Date = retValue.returnObject.Login_This_Date;
                        // _PageData.Create_Date = retValue.returnObject.Create_Date;
                        //_PageData.Status = retValue.returnObject.Status;

                        callback(null);
                    });
                },
                // 艺人信息列表
                function (callback) {
                    DALFactory.User.User_Anchor.Info(_PageData.User_ID, function (retValue) {
                        if (retValue.hasError) {
                            callback(retValue);
                            return;
                        }
                        _PageData.RoomName = retValue.returnObject.RoomName;
                        _PageData.RoomWelcome = retValue.returnObject.RoomWelcome;
                        _PageData.Tags = retValue.returnObject.Tags;
                        _PageData.Anchor_EXP = retValue.returnObject.EXP;
                        _PageData.Pomelo = retValue.returnObject.Pomelo;
                        _PageData.Pomelo_Today = retValue.returnObject.Pomelo_Today;
                        _PageData.Pomelo_Week = retValue.returnObject.Pomelo_Week;
                        _PageData.Pomelo_Month = retValue.returnObject.Pomelo_Month;
                        _PageData.Pomelo_Year = retValue.returnObject.Pomelo_Year;
                        _PageData.Live = retValue.returnObject.Live;
                        _PageData.Live_StartTime = retValue.returnObject.Live_StartTime;
                        _PageData.Live_Order_No = retValue.returnObject.Live_Order_No;
                        _PageData.Live_Duration_Total = retValue.returnObject.Live_Duration_Total;
                        _PageData.IDCard = retValue.returnObject.IDCard;
                        _PageData.Anchor_Phone = retValue.returnObject.Phone;
                        _PageData.Anchor_Email = retValue.returnObject.Email;
                        _PageData.QQ = retValue.returnObject.QQ;
                        _PageData.Network = retValue.returnObject.Network;
                        _PageData.Account_BankName = retValue.returnObject.Account_BankName;
                        _PageData.Account_BankFullName = retValue.returnObject.Account_BankFullName;
                        _PageData.Account_RealName = retValue.returnObject.Account_RealName;
                        _PageData.Account_CardNo = retValue.returnObject.Account_CardNo;
                        _PageData.List_Order = retValue.returnObject.List_Order;
                        _PageData.Anchor_Create_Date = retValue.returnObject.Create_Date;
                        _PageData.Anchor_Status = retValue.returnObject.Status;
                        _PageData.Anchor_Level = retValue.returnObject.Level;
                        _PageData.Level_Next = retValue.returnObject.Level_Next;
                        _PageData.EXP_Next = retValue.returnObject.EXP_Next;
                        _PageData.EXP_Difference = retValue.returnObject.EXP_Difference;

                        callback(null);
                    });
                }
            ], function (retValue) {
                if (retValue) {
                    _PageData.flag = retValue.message;
                    CommonUtils.Common.PageScript_PageData(res, _PageData);
                    res.render("finance-manage/anchor-manage/cash-edit", callback);
                    return;
                }
                CommonUtils.Common.PageScript_PageData(res, _PageData);
                res.render("finance-manage/anchor-manage/cash-edit", callback);
            })
        }
    },
    //艺人结算统计
    AnchorStat_Manage: {
        /////////////////////////////
        //艺人基本工资查询列表
        ////////////////////////////
        List: function (req, res, callback) {
            res.render("finance-manage/anchorstat-manage/list", callback);
        },
        /////////////////////////////
        //艺人基本工资查询导出
        ////////////////////////////
        Anchor_Stat_Excel: function (req, res, callback) {
            req.params.iDisplayLength = 100000;//默认十万条
            var _UserData = StateData.Value(req, res);//取得状态
            var reqModel = CommonUtils.Common.AjaxPage.Request(req);
            //判断登录
            if (!_UserData.IsLogin) {
                res.jsonp(CommonUtils.Common.AjaxPage.ErrorCode("请登录后使用此功能", reqModel.Echo));
                return;
            }
            //判断权限
            if (!_UserData.IsAdmin) {
                res.jsonp(CommonUtils.Common.AjaxPage.ErrorCode("没有权限", reqModel.Echo));
                return;
            }
            //查询参数
            var reqParm = new DALFactory.Report.Page.AdminAnchorStatistics_Parm();
            if (reqModel.Filter_Value.length > 0) {
                switch (reqModel.Filter_Type) {
                    case "User_ID":
                        reqParm.User_ID = reqModel.Filter_Value;
                        break;
                    case "NickName":
                        reqParm.NickName = reqModel.Filter_Value;
                        break;
                    case "Family_ID":
                        reqParm.Family_ID = reqModel.Filter_Value;
                        break;
                    default:
                        reqParm.FuzzyQuery = reqModel.Filter_Value;
                        break;
                }
            }
            reqParm.User_ID = req.params.F_User_ID ? KOC.StringUtils.ToInt(req.params.F_User_ID) : (reqParm.User_ID ? reqParm.User_ID : null);
            reqParm.NickName = req.params.F_NickName ? req.params.F_NickName : null;
            reqParm.Family_ID = req.params.F_Family_ID ? KOC.StringUtils.ToInt(req.params.F_Family_ID) : (reqParm.Family_ID ? reqParm.Family_ID : null);
            reqParm.Create_Date_Between = req.params.F_StartDate ? req.params.F_StartDate : null;
            reqParm.Create_Date_And = req.params.F_EndDate ? req.params.F_EndDate : null;
            //排序
            switch (reqModel.Sort_Col) {
                case "User_ID":
                case "NickName":
                case "Family_ID":
                    break;
                case "Create_Date":
                    reqModel.Sort_Col = "`User_ID`";
                    break;
                default:
                    reqModel.Sort_Col = "`User_ID` DESC";
                    reqModel.Sort_Dir = "";
                    break;
            }
            DALFactory.Report.Page.AdminAnchorStatistics(reqParm, reqModel, function (retValue) {
                if (retValue.hasError) {
                    res.jsonp(CommonUtils.Common.AjaxPage.ErrorCode(retValue.message + "(" + retValue.errorCode + ")", reqModel.Echo));
                    return;
                }
                var conf = {};
                conf.cols = [
                    {caption: '用户ID', type: 'number'},
                    {caption: '用户昵称', type: 'string'},
                    {caption: '公会ID', type: 'number'},
                    {caption: '艺人累计金豆收入', type: 'number'},
                    {caption: '艺人直播累计时长', type: 'number'},
                    {caption: '累计有效天数', type: 'number'}
                ];
                conf.rows = [];
                KOC.Lodash.forEach(retValue.returnObject, function (ThisValue) {
                    conf.rows.push([
                        ThisValue.User_ID,
                        ThisValue.NickName,
                        ThisValue.Family_ID,
                        ThisValue.Sum_BeansAnchor,
                        ThisValue.Sum_LiveDuring,
                        ThisValue.Sum_Efftive
                    ]);
                });
                var result = ExcelExport.execute(conf);
                res.setHeader('Content-Type', 'application/vnd.openxmlformats');
                res.setHeader("Content-Disposition", "attachment; filename=" + "ExportExcel.xlsx");
                res.end(result, 'binary');
            });
        }
    },
    //公会结算
    Family_Manage: {
        /////////////////////////////
        //公会结算列表
        ////////////////////////////
        Cash: function (req, res, callback) {
            res.render("finance-manage/family-manage/cash", callback);
        },
        /////////////////////////////
        //公会结算
        ////////////////////////////
        Cash_Edit: function (req, res, callback) {
            var _PageData = {
                flag: 0,
                Order_No: KOC.StringUtils.ToString(req.params.id, -1)
            };
            if (_PageData.Order_No <= 0) {
                CommonUtils.Common.PageScript_PageData(res, _PageData);
                res.render("finance-manage/family-manage/cash-edit", callback);
                return;
            }
            KOC.Async.series([
                // 公会信息列表
                function (callback) {
                    DALFactory.User.Family_Info.Info(_PageData.Family_ID, function (retValue) {
                        if (retValue.hasError) {
                            callback(retValue);
                            return;
                        }
                        // _PageData.Family_ID = retValue.returnObject.Family_ID;
                        _PageData.Family_Name = retValue.returnObject.Family_Name;
                        _PageData.User_ID = retValue.returnObject.User_ID;
                        _PageData.Account_BankName = retValue.returnObject.Account_BankName;
                        _PageData.Account_BankFullName = retValue.returnObject.Account_BankFullName;
                        _PageData.Account_RealName = retValue.returnObject.Account_RealName;
                        _PageData.Account_CardNo = retValue.returnObject.Account_CardNo;
                        _PageData.Beans = retValue.returnObject.Beans;
                        _PageData.Beans_Total = retValue.returnObject.Beans_Total;
                        _PageData.Family_Status = retValue.returnObject.Status;
                        _PageData.Anchor_Num = retValue.returnObject.Anchor_Num;
                        _PageData.ContactName = retValue.returnObject.ContactName;
                        _PageData.IDCard = retValue.returnObject.IDCard;
                        _PageData.Phone = retValue.returnObject.Phone;
                        _PageData.Email = retValue.returnObject.Email;
                        _PageData.QQ = retValue.returnObject.QQ;
                        // _PageData.Create_Date = retValue.returnObject.Create_Date;
                        // _PageData.Create_IP = retValue.returnObject.Create_IP;
                        callback(null);
                    });
                },
                //用户信息列表
                function (callback) {
                    DALFactory.User.User_Info.Info(_PageData.User_ID, function (retValue) {
                        if (retValue.hasError) {
                            callback(retValue);
                            return;
                        }
                        // _PageData.User_ID = retValue.returnObject.User_ID;
                        _PageData.NickName = retValue.returnObject.NickName;
                        _PageData.Sex = retValue.returnObject.Sex;
                        _PageData.RealName = retValue.returnObject.RealName;
                        // _PageData.Email = retValue.returnObject.Email;
                        // _PageData.Phone = retValue.returnObject.Phone;
                        _PageData.VIP = retValue.returnObject.VIP && (!retValue.returnObject.VIP_Expire || KOC.Moment().isBefore(retValue.returnObject.VIP_Expire));
                        _PageData.EXP = retValue.returnObject.EXP;
                        _PageData.Level = CommonUtils.User.Level.fnLevel(retValue.returnObject.EXP);
                        _PageData.Coin = retValue.returnObject.Coin;
                        _PageData.Coin_Today = retValue.returnObject.Coin_Today;
                        _PageData.Coin_Week = retValue.returnObject.Coin_Week;
                        _PageData.Coin_Month = retValue.returnObject.Coin_Month;
                        _PageData.Coin_Year = retValue.returnObject.Coin_Year;
                        _PageData.Coin_Total = retValue.returnObject.Coin_Total;
                        // _PageData.Beans = retValue.returnObject.Beans;
                        _PageData.Beans_Today = retValue.returnObject.Beans_Today;
                        _PageData.Beans_Week = retValue.returnObject.Beans_Week;
                        _PageData.Beans_Month = retValue.returnObject.Beans_Month;
                        _PageData.Beans_Year = retValue.returnObject.Beans_Year;
                        // _PageData.Beans_Total = retValue.returnObject.Beans_Total;
                        _PageData.Fans = retValue.returnObject.Fans;
                        _PageData.Attention = retValue.returnObject.Attention;
                        _PageData.IsAnchor = retValue.returnObject.IsAnchor;
                        _PageData.IsFamily = retValue.returnObject.IsFamily;
                        _PageData.Login_Count = retValue.returnObject.Login_Count;
                        _PageData.Login_This_Date = retValue.returnObject.Login_This_Date;
                        // _PageData.Create_Date = retValue.returnObject.Create_Date;
                        //_PageData.Status = retValue.returnObject.Status;
                        callback(null);
                    });
                },

            ], function (retValue) {
                if (retValue) {
                    _PageData.flag = retValue.message;
                    CommonUtils.Common.PageScript_PageData(res, _PageData);
                    res.render("finance-manage/family-manage/cash-edit", callback);
                    return;
                }
                CommonUtils.Common.PageScript_PageData(res, _PageData);
                res.render("finance-manage/family-manage/cash-edit", callback);
            })
        }
    },
    //公会结算统计
    FamilyStat_Manage: {
        /////////////////////////////
        //公会结算查询
        ////////////////////////////
        List: function (req, res, callback) {
            res.render("finance-manage/familystat-manage/list", callback);
        }
    }
};